package br.com.qualityempresa.seguranca

import java.security.MessageDigest

import sun.misc.BASE64Encoder
import cr.co.arquetipos.crypto.Blowfish

class PasswordCodec {

	static hashEncode = { String str ->
		MessageDigest md = MessageDigest.getInstance('SHA')
		md.update(str.getBytes('UTF-8'))
		return (new BASE64Encoder()).encode(md.digest())
	}
	
	static encode(target) {
		return Blowfish.encryptBase64(target, getPassword())
	}

	static decode(target) {
		return Blowfish.decryptBase64(target, getPassword())
	}

	private static getPassword() { 
		return 'madminsecret'
	}
	
	static int showRandomInteger(int aStart, int aEnd, Random aRandom){
		if ( aStart > aEnd ) {
		  throw new IllegalArgumentException("Start cannot exceed End.");
		}
		long range = (long)aEnd - (long)aStart + 1;
		long fraction = (long)(range * aRandom.nextDouble());
		return (int)(fraction + aStart);
	  }
}
